﻿using CTP.CRM.Core.MongoDB.Options;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace CTP.CRM.Core.MongoDB.Services
{
    public class ConnectionFactory
    {
        private static Lazy<MongoClient> _client;

        private readonly string _connectionString;

        private readonly string _dataBaseName;

        public ConnectionFactory(IOptions<MongoDBOptions> options)
        {
            _connectionString = options.Value.ConnectionString;
            _dataBaseName = options.Value.DataBaseName;
            _client = _client ?? new Lazy<MongoClient>(() => new MongoClient(_connectionString));
        }

        public IMongoDatabase GetDatabase()
        {
            return GetClient().GetDatabase(_dataBaseName);
        }

        public MongoClient GetClient()
        {
            return _client.Value;
        }
    }
}
